package com.cswangdao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cswangdao.domain.model.MarketCategory;
import com.cswangdao.domain.vo.CategoryL1Vo;
import com.cswangdao.domain.vo.CategoryVo;
import com.cswangdao.domain.vo.WXCatalogIndexVo;
import com.cswangdao.mapper.MarketCategoryMapper;
import com.cswangdao.result.PageResult;
import com.cswangdao.service.CategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private MarketCategoryMapper categoryMapper;

    @Override
    public PageResult<CategoryVo> list() {
        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession(true))*/{
      //      MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
            List<CategoryVo> levelL1 = categoryMapper.getByLevel("L1");
            List<CategoryVo> levelL2 = categoryMapper.getByLevel("L2");
            Map<Integer, List<CategoryVo>> collect = levelL2.stream().collect(Collectors.groupingBy(CategoryVo::getPid));
            levelL1.forEach(o->o.setChildren(collect.get(o.getId())));
            PageResult<CategoryVo> pageResult = PageResult.builder()
                    .page(1)
                    .limit(levelL1.size())
                    .total((long) levelL1.size())
                    .list((List) levelL1)
                    .pages(1)
                    .build();
            return pageResult;
        } catch (Exception e) {
           e.printStackTrace();
           return null;
        }
    }

    @Override
    public PageResult<CategoryL1Vo> L1Info() {
        try/*(SqlSession sqlSession = MybatisUtil.getSqlSession(true))*/ {
       //     MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
            List<CategoryVo> levelL1 = categoryMapper.getByLevel("L1");
            List<CategoryL1Vo> list = levelL1.stream().map(new Function<CategoryVo, CategoryL1Vo>() {
                @Override
                public CategoryL1Vo apply(CategoryVo categoryVo) {
                    CategoryL1Vo l1 = new CategoryL1Vo();
                    l1.setValue(categoryVo.getId());
                    l1.setLabel(categoryVo.getName());
                    return l1;
                }
            }).toList();
            PageResult<CategoryL1Vo> pageResult = PageResult.builder()
                    .page(1)
                    .total(Long.valueOf(list.size()))
                    .list((List) list)
                    .pages(1)
                    .limit(list.size())
                    .build();
            return pageResult;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public MarketCategory addOne(MarketCategory category) {
        try /*(SqlSession sqlSession = MybatisUtil.getSqlSession();)*/{
            Date now = new Date();
            category.setAddTime(now);
            category.setUpdateTime(now);

         //   MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
            categoryMapper.addOne(category);
         //   sqlSession.commit();
            return category;
        } catch (Exception e) {
            e.printStackTrace();
           return null;
        }
    }

    @Override
    public boolean updateById(MarketCategory category) {
        try/*( SqlSession sqlSession = MybatisUtil.getSqlSession();)*/ {
            category.setUpdateTime(new Date());

       //     MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
            categoryMapper.updateById(category);
        //    sqlSession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteById(Integer id) {
        try/* ( SqlSession sqlSession = MybatisUtil.getSqlSession();)*/{
          //  MarketCategoryMapper categoryMapper = sqlSession.getMapper(MarketCategoryMapper.class);
            categoryMapper.deleteById(id);
          //sqlSession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public WXCatalogIndexVo getCatalogIndex() {
        WXCatalogIndexVo wxCatalogIndexVo = new WXCatalogIndexVo();
        List<CategoryVo> levelL1 = categoryMapper.getByLevel("L1");
        List<MarketCategory> categoryList = BeanUtil.copyToList(levelL1, MarketCategory.class);
        MarketCategory currentCategory = categoryList.get(0);
        List<MarketCategory> currentSubCategory = categoryMapper.getByPid(currentCategory.getId());
        wxCatalogIndexVo.setCategoryList(categoryList);
        wxCatalogIndexVo.setCurrentCategory(currentCategory);
        wxCatalogIndexVo.setCurrentSubCategory(currentSubCategory);
        return wxCatalogIndexVo;
    }

    @Override
    public WXCatalogIndexVo getCurrenCatalogByPid(int pid) {
        WXCatalogIndexVo wxCatalogIndexVo = new WXCatalogIndexVo();
        MarketCategory category = categoryMapper.getById(pid);
        List<MarketCategory> currentSubCategory = categoryMapper.getByPid(pid);
        wxCatalogIndexVo.setCurrentCategory(category);
        wxCatalogIndexVo.setCurrentSubCategory(currentSubCategory);
        return wxCatalogIndexVo;
    }

}
